package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.pojos.WmNews;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class ArticleUpOrDownListener {
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = "upOrDown")
    public void onMessage(String message){
        if(!StringUtils.isEmpty(message)){
            System.err.println(message);
            WmNews wmNews = JSON.parseObject(message, WmNews.class);
            boolean isDown = true;
            if (wmNews.getEnable() == 1) {
                isDown = false;
            }

            // 根据文章id查询到文章配置,设置文章的状态
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .eq(ApArticleConfig::getArticleId, wmNews.getArticleId())
                    .set(ApArticleConfig::getIsDown, isDown)
            );
        }
    }
}
